package workexp.alg.question;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class IsAnagram {

    public boolean isAnagram(String s, String t) {
        HashMap<Character,Integer> strcount1 = new HashMap<>();
        HashMap<Character,Integer> strcount2 = new HashMap<>();
        for (char c : s.toCharArray()) {
            strcount1.put(c,strcount1.getOrDefault(c,0)+1);
        }

        for (char c : t.toCharArray()) {
            strcount2.put(c,strcount2.getOrDefault(c,0)+1);
        }

        if(s.length()==t.length()){
            for (Map.Entry<Character, Integer> characterIntegerEntry : strcount1.entrySet()) {
                if(!strcount2.getOrDefault(characterIntegerEntry.getKey(),0).equals(characterIntegerEntry.getValue())){
                    return false;
                }
            }
            return true;
        }else{
            return false;
        }
    }

    @Test
    public void test(){
        String s1="rat";
        String s2="car";
        boolean res= isAnagram(s1,s2);
        System.out.println(res);

    }
}
